package response

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

func WriteRawResp(c *gin.Context, code int, data interface{}, msg string) {
	c.JSON(http.StatusOK, NewResp(RespCode(code), data, msg))
}

// 自定义response
func WriteBadRequestResp(c *gin.Context, msg string) {
	c.JSON(http.StatusBadRequest, Response{
		Code: RespFailed,
		Msg:  msg,
	})
}

// 自定义response
func WriteResp(c *gin.Context, resp Response) {
	c.JSON(http.StatusOK, resp)
}

// 成功 response
func WriteOKResp(c *gin.Context, data interface{}) {
	c.JSON(http.StatusOK, Response{
		Code: RespCodeOK,
		Msg:  responseText[RespCodeOK],
		Data: data,
	})
}

// 失败response
func WriteFailedResp(c *gin.Context, data interface{}) {
	c.JSON(http.StatusOK, Response{
		Code: RespFailed,
		Msg:  responseText[RespFailed],
		Data: data,
	})
}

func WriteRespWithErr(c *gin.Context, resp Response, err error) {
	if err != nil {
		c.JSON(http.StatusOK, resp.SetCode(http.StatusInternalServerError).SetMsg(err.Error()))
		return
	}
	c.JSON(http.StatusOK, resp)
}

func WriteDataWithError(c *gin.Context, data interface{}, err error) {
	if err != nil {
		c.JSON(http.StatusOK, map[string]interface{}{
			"code": http.StatusInternalServerError,
			"data": data,
			"msg":  err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, map[string]interface{}{
		"code": http.StatusOK,
		"data": data,
		"msg":  "请求成功",
	})
}
